const mongoose = require("mongoose");

module.exports.add = async (data) => {
    // console.log(11111111111, data);

    return await mongoose.model("goodsModel").create(data);
};

module.exports.dle = async (data) => {
    // console.log(11111111111, data);

    return await mongoose.model("goodsModel").deleteOne(data);
};

//修改商品
module.exports.update = async (data) => {
    // console.log(11111111111, data);

    return await mongoose.model("goodsModel").update({ _id: data._id }, data);
};
// module.exports.getlist = async (data) => {
//     console.log(11111111111,data);

//     return await mongoose.model("goodsModel").find(data);
//  };

module.exports.getlist = async (data) => {
    // 获取总条数
    // console.log(777777777777777, data);

    const { currentPage, pageSize } = data
    const totalCount = await mongoose.model("goodsModel").countDocuments();
    // 总页数
    const totalPages = Math.ceil(totalCount / pageSize);
    // 获取学生数据
    if (data.type) {
        const goodsData = await mongoose.model("goodsModel")
        .find({
            [data.type]: { $regex: data.value }
         })
       
            .limit(pageSize - 0)  // 查询条数
            .skip((currentPage - 1) * pageSize)  // 跳过条数
            .populate("adminsId")
          
        return { currentPage, pageSize, totalCount, totalPages, goodsData }

    }
    else {
        const goodsData = await mongoose.model("goodsModel")
            .find()
            .limit(pageSize - 0)  // 查询条数
            .skip((currentPage - 1) * pageSize)  // 跳过条数
            .populate("adminsId")
        return { currentPage, pageSize, totalCount, totalPages, goodsData }
    }
    
}